package org.docmanagement.actions;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.docmanagement.commons.struts.CommonAction;
import org.docmanagement.data.Book;
import org.docmanagement.data.Category;
import org.docmanagement.data.Element;
import org.docmanagement.data.File;
import org.docmanagement.data.Keyword;
import org.docmanagement.data.Link;
import org.docmanagement.forms.MultipleElementForm;

/**
 * Created on 15 nov. 2004
 * @author eveno
 */
public class MassiveAddAction extends CommonAction
{

    /**
     * @see org.docmanagement.commons.struts.BaseDispatchAction#createMethodMap()
     */
    protected void createMethodMap()
    {
        methodMap = new HashMap<String, String>();
        methodMap.put("MassiveAdd", "massiveAdd");
        methodMap.put("MassiveAdd", "massiveAdd");
        methodMap.put("PrepareMassiveAdd", "getRessources");
    }
    /**
     * 
     */
    public ActionForward massiveAdd(ActionMapping mapping, ActionForm form, HttpServletRequest request,
            HttpServletResponse response) throws Exception
    {
        MultipleElementForm elements = (MultipleElementForm) form;
        int elementCount = elements.getElementcount().intValue();
        for (int i = 0; i < elementCount; i++)
        {
        	boolean chosen = Boolean.valueOf(request.getParameter("chosen"+i));
            if (!chosen)
                continue;
            Element e = null;
            if (elements.getType() == FILE_TYPE)
                e = createFile(request, i);
            if (elements.getType() == BOOK_TYPE)
                e = createBook(request, i);
            if (elements.getType() == LINK_TYPE)
                e = createLink(request, i);
            fillElement(e, request, i);
            log.debug(e.toCompleteString());
            getElementService().add(e);
        }
	    addElements(request, getElementService().findWithNoCategory());
        return mapping.findForward(SHOW);
    }
    /**
     * 
     */
    public Element createBook(HttpServletRequest request, int i) throws Exception
    {
        log.info("Creating book element");
        return new Book();
    }
    /**
     * 
     */
    public Element createFile(HttpServletRequest request, int i) throws Exception
    {
        String filename = request.getParameter("filename"+i);
        log.info("Creating file element");
        File f = new File(filename);
        log.debug("Setting main category for file");
        String mainCategoryIdString = request.getParameter("mainCategoryId"+i);
        Category c = (Category) getCategoryService().findById( new Integer(mainCategoryIdString) );
        f.setMainCategory(c);
        log.debug("Adding file to repository");
        getRepositoryService().addFile( f );
        log.debug("File stored in "+f.getFile());
        return f;
    }
    /**
     * 
     */
    public Element createLink(HttpServletRequest request, int i) throws Exception
    {
        String url = request.getParameter("url"+i);
        log.info("Creating link element with url "+url);
        Link l = new Link(url);
        return l;
    }
    /**
     * @param e
     * @param request
     */
    private void fillElement(Element e, HttpServletRequest request, int i) throws Exception
    {
        e.setTitle(request.getParameter("title"+i));
        e.setDescription(request.getParameter("description"+i));
        // Get category id list
        String[] ids = request.getParameterValues("categoryIds"+i);
        List<Category> categories = getCategoryService().findAll( toIntegerArray(ids) );
        e.setCategories(categories);
        // Get keyword id list
        ids = request.getParameterValues("keywordIds"+i);
        List<Keyword> keywords = getKeywordService().findAll( toIntegerArray(ids) );
        e.setKeywords(keywords);
    }
    /**
     * @param ids
     * @return
     */
    private Integer[] toIntegerArray(String[] array)
    {
        Integer[] ids = new Integer[array.length];
        for (int i = 0; i < array.length; i++)
        {
            ids[i] = new Integer(array[i]);
        }
        return ids;
    }
}
